#!/bin/bash

if [[ $(id -u) != 0 ]]; then
    echo "Please run as root or use sudo"
    exit 1
fi

PKGS=""

PKGS="${PKGS} qemu-system-arm"
PKGS="${PKGS} qemu-kvm"
PKGS="${PKGS} libvirt-clients"
PKGS="${PKGS} libvirt-daemon-system"
PKGS="${PKGS} bridge-utils"
# PKGS="${PKGS} virt-manager"

# PKGS="${PKGS} qemu-system-misc"
# PKGS="${PKGS} qemu-user"
# PKGS="${PKGS} qemu-user-static"
# PKGS="${PKGS} binfmt-support"

SUCCESSED=""
FAILED=""

for pkg in ${PKGS}; do
    echo "Installing ${pkg}"
    apt-get install -y ${pkg} \
        && SUCCESSED="${SUCCESSED} ${pkg}" \
        || FAILED="${FAILED} ${pkg}"
done

echo "SUCCESSED: " ${SUCCESSED}
echo "FAILED: " ${FAILED}

modprobe kvm

systemctl status --no-pager libvirtd
systemctl start libvirtd
systemctl enable libvirtd

# fix: error: no active connection to install on
chmod 777 /var/run/libvirt/libvirt-sock

# opt 1:
# sudo usermod -a -G libvirt $(whoami)
# sudo usermod -a -G kvm $(whoami)
# opt 2:
# newgrp libvirt

